package com.mac.intellect_flow.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mac.intellect_flow.reply.PageParam;
import com.mac.intellect_flow.reply.PageResult;
import com.mac.intellect_flow.sys.entity.SysExpression;
import com.mac.intellect_flow.sys.mapper.SysExpressionMapper;
import com.mac.intellect_flow.sys.service.SysExpressionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * 流程达式Service业务层处理
 * 
 * @author mac
 */
@Service
public class SysExpressionServiceImpl implements SysExpressionService {
    @Autowired
    private SysExpressionMapper sysExpressionMapper;

    /**
     * 查询流程达式
     * 
     * @param id 流程达式主键
     * @return 流程达式
     */
    @Override
    public SysExpression selectSysExpressionById(Long id)
    {
        return sysExpressionMapper.selectSysExpressionById(id);
    }

    /**
     * 查询流程达式列表
     *
     * @param sysExpression 流程达式
     * @param pageParam
     * @return 流程达式
     */
    @Override
    public PageResult<SysExpression> queryPage(SysExpression sysExpression, PageParam pageParam) {
        Page<SysExpression> page = new Page<>(pageParam.getPageNo(), pageParam.getPageSize());
        LambdaQueryWrapper<SysExpression> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(sysExpression.getStatus() != null,SysExpression::getStatus, sysExpression.getStatus())
                        .like(StringUtils.isNotEmpty(sysExpression.getName()),SysExpression::getName, sysExpression.getName())
                         .eq(StringUtils.isNotEmpty(sysExpression.getExpression()), SysExpression::getExpression, sysExpression.getExpression());
        Page<SysExpression> sysExpressionPage = sysExpressionMapper.selectPage(page, queryWrapper);
        return new PageResult<>(sysExpressionPage.getRecords(),sysExpressionPage.getTotal(),pageParam.getPageSize(), pageParam.getPageNo());
    }

    @Override
    public List<SysExpression> selectSysExpressionList(SysExpression sysExpression) {
        return sysExpressionMapper.selectSysExpressionList(sysExpression);
    }

    /**
     * 新增流程达式
     * 
     * @param sysExpression 流程达式
     * @return 结果
     */
    @Override
    public int insertSysExpression(SysExpression sysExpression) {
        sysExpression.setCreateTime(LocalDateTime.now());
        return sysExpressionMapper.insertSysExpression(sysExpression);
    }

    /**
     * 修改流程达式
     * 
     * @param sysExpression 流程达式
     * @return 结果
     */
    @Override
    public int updateSysExpression(SysExpression sysExpression) {
        sysExpression.setUpdateTime(LocalDateTime.now());
        return sysExpressionMapper.updateSysExpression(sysExpression);
    }

    /**
     * 批量删除流程达式
     * 
     * @param ids 需要删除的流程达式主键
     * @return 结果
     */
    @Override
    public int deleteSysExpressionByIds(Long[] ids)
    {
        return sysExpressionMapper.deleteSysExpressionByIds(ids);
    }

    /**
     * 删除流程达式信息
     * 
     * @param id 流程达式主键
     * @return 结果
     */
    @Override
    public int deleteSysExpressionById(Long id)
    {
        return sysExpressionMapper.deleteSysExpressionById(id);
    }
}
